package com.tf.six.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tf.six.entity.ProductSP;
import com.tf.six.service.ProductSPService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/products")
@Tag(name = "产品接口", description = "符合Restful风格的产品接口")
public class ProductSPController {

    @Autowired
    private ProductSPService productSPService;

    @Operation(summary = "查询所有产品", description = "查询所有产品")
    @GetMapping
    @ApiResponse(responseCode = "200", description = "查询所有产品返回产品list")
    public List<ProductSP> getAllProducts() {
        return productSPService.list();
    }

    @Operation(summary = "根据id查询产品", description = "根据id查询产品")
    @Cacheable(value = "productCache", key = "#id")
    @GetMapping("/{id}/{name}")
    public ProductSP getProductById(@Parameter(description = "产品id", required = true) @PathVariable("id") Integer id,
                                    @PathVariable("name") String name) {
        log.error("id:{},name:{}", id, name);
        return productSPService.getById(id);
    }

    @Operation(summary = "分页查询产品", description = "分页查询产品")
    @GetMapping("/querypage/{page}/{size}")
    public List<ProductSP> queryPage(@PathVariable("page") Integer page,
                                     @PathVariable("size") Integer size) {
        Page<ProductSP> testpage = new Page<>(page, size);
        Page<ProductSP> productPage = productSPService.page(testpage, null);
        return productPage.getRecords();
    }

    @Operation(summary = "分页查询产品", description = "分页查询产品")
    @GetMapping("/querylist/{page}/{size}")
    public List<ProductSP> queryList(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String description) {

        QueryWrapper<ProductSP> queryWrapper = new QueryWrapper<>();
        if (name != null) {
            queryWrapper.like("name", name);
        }
        if (description != null) {
            queryWrapper.like("description", description);
        }

        Page<ProductSP> testpage = new Page<>(page, size);
        Page<ProductSP> productPage = productSPService.page(testpage, queryWrapper);
        return productPage.getRecords();
    }

    @Operation(summary = "添加产品", description = "添加产品")
    @PostMapping
    public boolean addProduct(@RequestBody ProductSP productSP) {
        return productSPService.save(productSP);
    }

    @Operation(summary = "修改产品", description = "修改产品")
    @PutMapping
    public boolean updateProduct(@RequestBody ProductSP productSP) {
        return productSPService.updateById(productSP);
    }

    @Operation(summary = "删除产品", description = "删除产品")
    @DeleteMapping("/{id}")
    public boolean deleteProduct(@PathVariable("id") Integer id) {
        return productSPService.removeById(id);
    }
}